<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>财务</a>
        <a>
          <cite>业绩分配</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="achievement" class="x-body" class="clear:both;">

        <div class="layui-row layui-col-space10">
            <div class="layui-col-md6">
                <fieldset class="layui-elem-field layui-field-title" >
                    <legend style="font-size: 18px;">校区业绩分配</legend>
                </fieldset>
                <div class="layui-form layui-col-md12 x-so layui-form-pane">
                    <div class="layui-input-inline">
                      <!--  <input type="text" name="campus_name" placeholder="请输入校区名字" autocomplete="off" class="layui-input"> -->
                        <select name="campus_name" lay-search="" lay-verify="required">
                            <option selected value="">请选择校区</option>
                            {foreach name="campus_name" id="vo"}
                            <option value="{$vo.campus_name}">{$vo.campus_name}</option>
                            {/foreach}
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" style="width: auto;" placeholder="选择分配日期" name="allocation_campus_date" id="start">
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn" id="search_all">
                            <i class="layui-icon layui-icon-search"></i>筛选</button>
                        <button class="layui-btn layui-btn-danger" id="clear_all">
                            <i class="layui-icon"></i>重置</button>
                    </div>
                </div>

                <table class="layui-hide" id="LAY_table_campus" lay-filter="table"></table>
                <script type="text/html" id="barTable_campus">
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="adjustment_campus">调整业绩</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="del_campus">删除</a>
                </script>
            </div>

            <div class="layui-col-md6">
                <fieldset class="layui-elem-field layui-field-title" >
                    <legend style="font-size: 18px;">个人业绩分配</legend>
                </fieldset>
                <div class="layui-form layui-col-md12 x-so layui-form-pane">
                    <div class="layui-input-inline">
                        <!--
                        <input type="text" name="adjustment_name" placeholder="请输分配人名字" autocomplete="off" class="layui-input">
                        -->
                        <select name="adjustment_name" lay-search="" lay-verify="required">
                            <option value="">请输选择分配人</option>
                            {foreach name="data[25]" id="vo"}
                            <option value="{$vo.name}">{$vo.name}</option>
                            {/foreach}
                        </select>

                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" style="width: auto;" placeholder="选择分配日期" name="allocation_only_date" id="start_only">
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn" id="search_only">
                            <i class="layui-icon layui-icon-search"></i>筛选</button>
                        <button class="layui-btn layui-btn-danger" id="clear_only">
                            <i class="layui-icon"></i>重置</button>
                    </div>
                </div>

                <table class="layui-hide" id="LAY_table_only" lay-filter="table"></table>
                <script type="text/html" id="barTable_only">
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="adjustment_only">调整业绩</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="del_only">删除</a>
                </script>
            </div>
        </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>

    layui.use(['form','laydate','element', 'table', 'layer', 'laytpl'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table,
            element = layui.element,
            laydate = layui.laydate,
            layer = layui.layer;

        var loading = layer.load();
        //数据表格 方法级渲染
        var tableIns_campus = table.render({
            elem: '#LAY_table_campus'
            , url: '{:url("finance/campuslists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'id', title: 'ID', width: 50, align: 'center'
                },{field: 'campus_id_all_text', title: '校区', style:'color:#01AAED;font-weight: bold;', width: 100, align: 'center'
                },{field: 'contract_number', title: '分配订单编号', width: 200, align: 'center'
                },{field: 'assignment_all_text', title: '收费归属', width: 100, align: 'center'
                },{field: 'rate_all', title: '比率', width: 100, align: 'center'
                },{field: 'rate_all_money', title: '所分金额(元)',style:'color:#01AAED;font-weight: bold;', width: 120, align: 'center'
                },{field: 'amount_already_all', title: '付款金额(元)', style:'color:#FF5722;font-weight: bold;', width: 120, align: 'center'
                },{field: 'update_time', title: '生成时间', width: 220, align: 'center'
                },{fixed: 'right', title: '操作', width: 140, align: 'center', toolbar: '#barTable_campus'}
            ]]
            , height: 'full-300'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
            }
        });
        //日期
        laydate.render({
            elem: '#start',
            range: true
            ,trigger: 'click'
        });

        //日期
        laydate.render({
            elem: '#start_only',
            range: true
            ,trigger: 'click'
        });

        var tableIns_only = table.render({
            elem: '#LAY_table_only'
            , url: '{:url("finance/onlylists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'id', title: 'ID', width: 50, align: 'center'
                },{field: 'payee_only_text',style:'color:#01AAED;font-weight: bold;', title: '分配人', width: 100, align: 'center'
                },{field: 'contract_number', title: '分配订单编号', width: 200, align: 'center'
                },{field: 'assignment_only_text', title: '收费归属', width: 100, align: 'center'
                },{field: 'rate_only', title: '比率', width: 100, align: 'center'
                },{field: 'rate_only_money', title: '所分金额(元)', style:'color:#01AAED;font-weight: bold;', width: 120, align: 'center'
                },{field: 'amount_already_only', title: '付款金额(元)', style:'color:#FF5722;font-weight: bold;', width: 120, align: 'center'
                },{field: 'update_time', title: '生成时间', width: 220, align: 'center'
                },{fixed: 'right', title: '操作', width: 140, align: 'center', toolbar: '#barTable_only'}
            ]]
            , height: 'full-300'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
            }
        });


        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del_campus') { //删除
                layer.confirm('真的删除这条校区业绩么', function (index) {
                    $.post("{:url('finance/campus_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                            setTimeout(function () {
                                tableIns_campus.reload();
                            }, 1000)
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });

            }else if (layEvent === 'del_only') { //删除
                layer.confirm('真的删除这条个人业绩数据么', function (index) {
                    $.post("{:url('finance/only_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                            setTimeout(function () {
                                tableIns_only.reload();
                            }, 1000)
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });

            }else if (layEvent === 'adjustment_campus'){   //调整校区业绩

                let url = '{:url("Finance/performance_all")}?id='+ data.id;
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    title: '校区业绩调整',
                    closeBtn: 2,
                    shadeClose: false,
                    area: ['50%', '30%'],
                    content: url,
                    end:function () {

                        tableIns_campus.reload();
                    }
                });


            }else if (layEvent === 'adjustment_only'){   //调整个人业绩

                let url = '{:url("Finance/performance_only")}?id='+ data.id;
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    title: '个人业绩调整',
                    closeBtn: 2,
                    shadeClose: false,
                    area: ['50%', '30%'],
                    content: url,
                    end:function () {

                        tableIns_only.reload();
                    }
                });


            }

        });
        //搜索项校区业绩
        $('#search_all').click(function () {
            var field = {};
            field.campus_id_all_text = $('select[name=campus_name]').val();
            var create_date_rang = $('input[name=allocation_campus_date]').val();
            if(create_date_rang.length > 0){
                strs_date=create_date_rang.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }

            console.log(field);
            tableIns_campus.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });//清除校区业绩
        $('#clear_all').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns_campus.reload({
                where: {
                    'campus_id_all_text': '',
                    'allocation_campus_date':''
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //搜索项个人业绩
        $('#search_only').click(function () {
            var field = {};
            field.payee_only_text = $('select[name=adjustment_name]').val();
            var create_date_rang = $('input[name=allocation_only_date]').val();
            if(create_date_rang.length > 0){
                strs_date=create_date_rang.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }
            console.log(field);
            tableIns_only.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });//清除个人业绩
        $('#clear_only').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns_only.reload({
                where: {
                    'payee_only_text': '',
                    'allocation_only_date':''
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

    });

    //获取当前时间，格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
</script>